package com.lh.api.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

import static com.google.common.collect.Lists.newArrayList;

@Configuration
@EnableSwagger2
public class SwaggerConfig implements WebMvcConfigurer {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(apiInfo())
            .useDefaultResponseMessages(true)
            .select()
            //加了ApiOperation注解的类，才生成接口文档
            .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
            //包下的类，才生成接口文档
            //.apis(RequestHandlerSelectors.basePackage("com.lh.web.controller"))
            .paths(PathSelectors.any())
            .build()
            .globalOperationParameters(setHeaderToken())
            .securitySchemes(security());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
            .title("一元夺宝后端api接口")
            .description("国瑞-api文档以及处理状态码code值说明：</br>"
                + "0 -- 操作成功</br>"
                + "1000 -- 参数为空或非法</br>"
                + "1001 -- 添加失败</br>"
                + "1002 -- 更新失败</br>"
                + "1003 -- 删除失败</br>"
                + "1004 -- 操作失败</br>"
                + "1005 -- 已存在</br>"
                + "1006 -- 密码错误</br>"
                + "1007 -- 用户被锁定或者删除</br>"
                + "1008 -- 限制重复提交</br>"
                + "1009 -- 数据不存在</br>"
                + "1010 -- 两次输入不一样</br>"
                + "1011 -- 验证码错误</br>"
                + "1012 -- 无效</br>"
                + "9110 -- 非法操作</br>"
                + "9111 -- token签名与本地计算签名不匹配</br>"
                + "404 -- 找不到指定页面</br>"
                + "500 -- 服务器异常</br>"
                + "401 -- token过期，请重新登录</br>"
                + "9990 -- 系统异常</br>"
                + "9999 -- 未知异常</br>")
            .version("1.0.0")
            .build();
    }
    private static final String TOKEN = "token";

    private List<ApiKey> security() {
        return newArrayList(
            new ApiKey(TOKEN, TOKEN, "header")
        );
    }

    private List<Parameter> setHeaderToken() {
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        tokenPar.name(TOKEN).description("X-Auth-Token").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.build());
        return pars;
    }

}
